<?php
/**
 * slaves
 * 受控点
 * @author Administrator
 * @version
 */
class Model_Tables_Slaves extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'slaves';
    /**
     * add
     * 增加受控点
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'ip' => $params['ip'],
        'sip' => $params['sip'],
		'des' => $params['des'],
        //'liveport' => $params['liveport'],
        //'vodport' => $params['vodport'],
		//'tsport' => $params['tsport'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑受控点
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'ip' => $params['ip'],
        'sip' => $params['sip'],
		'des' => $params['des'],
        //'liveport' => $params['liveport'],
        //'vodport' => $params['vodport'],
		//'tsport' => $params['tsport'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑受控点
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转受控点
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update slaves set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除受控点
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取受控点信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取受控点数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("id=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取受控点数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    /*
     * 获取受控点列表
     * */
	public function getSlavesList()
    {
        $db = $this->getAdapter();		
		$sql = "select id,des from slaves";
		$result = $db->query($sql);
    	$types = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $types[$item['id']] = $item['des'];
	            }
	        }
        return $types;
    }
    /**
     * 获取受控点列表
     * */
	public function getAllSlavesList()
    {
        $db = $this->getAdapter();		
		$sql = "select * from slaves";
		$result = $db->query($sql);
    	$types = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $types[$item['id']] = $item;
	            }
	        }
        return $types;
    }
    /*
     * getSlavesSipList获取服务ip和id的对应关系数组
     * */
	public function getSlavesSipList()
    {
        $db = $this->getAdapter();		
		$sql = "select id,sip from slaves";
		$result = $db->query($sql);
    	$types = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $types[$item['id']] = $item['sip'];
	            }
	        }
        return $types;
    }
    public function getSlaveId($ip){
     	$db = $this->getAdapter();
    	$result = $db->fetchOne(
		    "SELECT id FROM slaves WHERE ip = :ip",
		    array('ip' => $ip)
		);
		return $result;
    }
}

